package com.yuntian.controller.user;

import com.yuntian.Dto.CartPageQueryDto;
import com.yuntian.entity.Cart;
import com.yuntian.result.PageResult;
import com.yuntian.result.Result;
import com.yuntian.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public Result<PageResult> getCartList(CartPageQueryDto cartPageQueryDto) {
        PageResult pageResult = cartService.selectAll(cartPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 加入购物车
     */
    @PostMapping("/add")
    public Result addToCart(@RequestBody Cart cart) {
        cartService.insertCart(cart);
        return Result.success();
    }

    /**
     * 修改购物车
     */
    @PutMapping("/update")
    public Result updateCart(@RequestBody Cart cart) {
        cartService.updateCart(cart);
        return Result.success();
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/delete/{cartId}")
    public Result deleteCart(@PathVariable Integer cartId) {
        cartService.deleteCart(cartId);
        return Result.success();
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clear")
    public Result clearCart(@RequestParam Long userId) {
        cartService.deleteCartByUserId(userId);
        return Result.success();
    }

    /**
     * 批量删除购物车
     */
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        cartService.deleteCartByIds(ids);
        return Result.success();
    }

}
